package dev.helper;

import java.util.List;

import eu.news.data.dao.DAOFactory;
import eu.news.data.dao.OperatorDAO;
import eu.news.data.dao.SortDAO;
import eu.news.data.dao.SpecialDAO;
import eu.news.data.pojo.Operator;
import eu.news.data.pojo.Sort;
import eu.news.data.pojo.Special;

public class DeployInitialize extends Thread{

	public static void main(String[] args) {
		updateOperator(updateSort(updateSpecial()));
	}

	public void run() {
		updateOperator(updateSort(updateSpecial()));
	}
	/**
	 * 建立默认专题
	 * 
	 * @return
	 */
	public static Special updateSpecial() {
		SpecialDAO spDao = DAOFactory.getSpecialDAO();
		if (spDao.getByName("新闻") != null)
			return spDao.getByName("新闻");
		Special ap = spDao.newEntity();
		ap.setName("新闻");
		ap.setTheme("default");
		spDao.save(ap);
		spDao.commit();
		return ap;
	}

	/**
	 * 更新专题
	 * @param sp
	 * @return
	 */
	public static List<Sort> updateSort(Special sp) {
		SortDAO dao = DAOFactory.getSortDAO();
		String[] names = { "校内新闻", "通知公告", "讲座信息", "人物访谈", "专题报导", "前沿知识",
				"他山之石" };

		for (String name : names) {
			Sort sort = dao.getByNameSpecial(name, sp);
			if (sort == null) {
				sort = dao.newEntity();
				sort.setName(name);
				sort.setSpecial(sp);
				dao.save(sort);
			}
		}
		dao.commit();
		return dao.getBySpecial(sp);
	}

	/**
	 * 新建Debug用户
	 * @param sorts
	 */
	public static void updateOperator(List<Sort> sorts) {
		OperatorDAO dao = DAOFactory.getOperatorDAO();
		Operator op = dao.getByName("debug");
		if (op != null)
			return;
		op = dao.newEntity();
		op.setName("debug");
		op.setType(Operator.TYPE.LOCAL_ADMIN);
		op.setSorts(sorts);
		dao.save(op);
		dao.commit();
	}

}
